package top.bug.blog.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import top.bug.blog.common.Result;
import top.bug.blog.enums.ErrorCode;

import javax.servlet.http.HttpServletRequest;

/**
 * @author <a href="https://github.com/jiusi66">九思.</a>
 * @email 10837907@qq.com
 * @from <a href="https://www.52bug.top">递归茶馆-www.52bug.top</a>
 * @description 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 自定义业务异常全局处理器
     */
    @ExceptionHandler(BussinessException.class)
    public Result<Void> errorHandler(HttpServletRequest request, BussinessException ex) {
        log.error("业务异常，请求地址：{}，错误信息：", request.getRequestURI(), ex);
        return Result.fail(ex.getCode(), ex.getMessage());
    }

    /**
     * 系统异常全局处理器
     */
    @ExceptionHandler(Exception.class)
    public Result<Void> errorHandler(HttpServletRequest request, Exception ex) {
        log.error("未知异常，请求方法：{}，请求地址：{}，错误信息：", request.getMethod(), request.getRequestURI(), ex);
        return Result.fail(ErrorCode.SYSTEM_ERROR);
    }
}